package com.sprouting.ops.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.sprouting.ops.interfaces.ConstantBase;
import com.sprouting.ops.model.OpsPersonalPassword;
import com.sprouting.ops.model.ResultJson;
import com.sprouting.ops.model.qo.OpsPersonalPasswordQo;
import com.sprouting.ops.service.OpsPersonalPasswordService;
import com.sprouting.ops.utils.CheckTool;
import com.sprouting.ops.utils.EntityTool;
import com.sprouting.ops.utils.IdTool;
import org.springframework.web.bind.annotation.*;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sprouting.ops.annotations.invoke.IdempotentSingle;
import lombok.extern.slf4j.Slf4j;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import com.sprouting.ops.interfaces.AddGroup;
import com.sprouting.ops.interfaces.UpdGroup;

/**
 * 个人密码箱(OpsPersonalPassword)(OPS_PERSONAL_PASSWORD) 表控制层
 *
 * @author ：lx（长沙麓谷）
 * @date 创建时间：2025-08-28 22:17:03
 */
@Slf4j
@RestController
@RequestMapping("opsPersonalPassword")
public class OpsPersonalPasswordController {

    @Resource
    private OpsPersonalPasswordService opsPersonalPasswordService;




    /**
     * 分页查询个人密码箱
     * @param query 查询参数
     * @return
     */
    @PostMapping("getListPage")
    public ResultJson getListPage(OpsPersonalPasswordQo query){
        if (query == null){
            query = new OpsPersonalPasswordQo();
        }
        query.setUserId(StpUtil.getLoginIdAsString());

        Page page = query.getPage();
        return ResultJson.success(opsPersonalPasswordService.getListPage(page, query));
    }

    /**
     * 删除个人密码箱
     * @param id 个人密码箱id
     * @return
     */
    @PostMapping("delOpsPersonalPassword")
    @IdempotentSingle
    public ResultJson delOpsPersonalPassword(String id){
        if (CheckTool.checkNull(id)){
            return ResultJson.errorCheck();
        }

        opsPersonalPasswordService.removeById(id);
        return ResultJson.success();
    }

    /**
     * 更新个人密码箱
     * @param bo 修改参数
     * @return
     */
    @PostMapping("updOpsPersonalPassword")
    @IdempotentSingle
    public ResultJson updOpsPersonalPassword(@Validated(UpdGroup.class) OpsPersonalPassword bo){
        String msg = checkOpsPersonalPassword(bo);
        if (msg != null){
            return ResultJson.error(msg);
        }

        EntityTool.updBase(bo);
        opsPersonalPasswordService.updateById(bo);
        return ResultJson.success(bo);
    }

    /**
     * 保存个人密码箱
     * @param bo 保存参数
     * @return
     */
    @PostMapping("saveOpsPersonalPassword")
    @IdempotentSingle
    public ResultJson saveOpsPersonalPassword(@Validated(AddGroup.class) OpsPersonalPassword bo){
        String msg = checkOpsPersonalPassword(bo);
        if (msg != null){
            return ResultJson.error(msg);
        }
        if (bo.getStatus() == null){
            bo.setStatus(ConstantBase.PasswordStatus.USE);
        }

        bo.setId(IdTool.getId());
        bo.setUserId(StpUtil.getLoginIdAsString());
        EntityTool.saveBase(bo);
        opsPersonalPasswordService.save(bo);
        return ResultJson.success(bo);
    }


    /**
     * 个人密码箱数据校验
     * @param bo 个人密码箱 校验数据
     * @return 非null则有错误
     */
    private String checkOpsPersonalPassword(OpsPersonalPassword bo){

        return null;
    }

}
